package com.winway.sixbook.ui;

import java.util.Calendar;
import com.winway.sixbook.R;
import com.winway.sixbook.service.SeedManager;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class ExportSet extends Activity {
	
	private EditText showDate = null;
	private Button pickDate = null;
	private EditText showDate2 = null;
	private Button pickDate2 = null;
	private EditText showTime = null;
	private Button pickTime = null;
	
	private static final int SHOW_DATAPICK = 0; 
    private static final int DATE_DIALOG_ID = 1;  
    private static final int SHOW_TIMEPICK = 2;
    private static final int TIME_DIALOG_ID = 3;
    private static final int SHOW_DATAPICK2 = 4; 
    private static final int DATE_DIALOG_ID2 = 5; 
    
    private int mYear;  
    private int mMonth;
    private int mDay; 
    private int mHour;
    private int mMinute;
    
    private ImageButton top_tbn_fanhui;
    private Button top_tbn_xinzeng;
	private TextView title,seedtype;
	
	private SeedManager mseedManager;
	private ArrayAdapter<String> adapter;
	private Spinner spinner1 ;
	private Button searchbtn;
	private String addtime;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_export_set);
        if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        if(null!= bundle && null != bundle.getString("Addtime")){
        	addtime = bundle.getString("Addtime");
        }
        
        initializeViews();
        
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);  
        mMonth = c.get(Calendar.MONTH);  
        mDay = c.get(Calendar.DAY_OF_MONTH);
        
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
        
        setDateTime(); 
        setTimeOfDay();
    }
    
    /**
     * 初始化控件和UI视图
     */
    private void initializeViews(){
        showDate = (EditText) findViewById(R.id.showdate);  
        pickDate = (Button) findViewById(R.id.pickdate); 
        showTime = (EditText)findViewById(R.id.showtime);
        pickTime = (Button)findViewById(R.id.picktime);
        showDate2 = (EditText) findViewById(R.id.showdate2);  
        pickDate2 = (Button) findViewById(R.id.pickdate2); 
        
        top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        seedtype =(TextView)findViewById(R.id.seedtypes);
        searchbtn =(Button)findViewById(R.id.searchbtn);
        
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
        title.setText("导出数据");
        
        setSpinner();
        
        pickDate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
	           Message msg = new Message(); 
	           if (pickDate.equals(v)) {  
	              msg.what = ExportSet.SHOW_DATAPICK;  
	           }  
	           ExportSet.this.dateandtimeHandler.sendMessage(msg); 
			}
		});
        pickDate2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
	           Message msg = new Message(); 
	           if (pickDate2.equals(v)) {  
	              msg.what = ExportSet.SHOW_DATAPICK2;  
	           }  
	           ExportSet.this.dateandtimeHandler.sendMessage(msg); 
			}
		});
        pickTime.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
	           Message msg = new Message(); 
	           if (pickTime.equals(v)) {  
	              msg.what = ExportSet.SHOW_TIMEPICK;  
	           }  
	           ExportSet.this.dateandtimeHandler.sendMessage(msg); 
			}
		});
        
        top_tbn_fanhui.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intentfh = new Intent(ExportSet.this, GridActivity.class);
				startActivity(intentfh);
				finish();
			}
		});
        
        searchbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent= new Intent(ExportSet.this, ExportDataActivity.class);
				Bundle bundle = new Bundle(); 
				String starttimeString = showDate.getText().toString().trim();
				String endtimeString = showDate2.getText().toString().trim();
				
				bundle.putString("starttime", starttimeString);
				bundle.putString("endtime", endtimeString);
				bundle.putString("seedtype", seedtype.getText().toString().trim());
				bundle.putString("Addtime", addtime);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
		});

    }
    
    private void setSpinner(){
    	//动态指定seedStrings
        final String[] seedStrings = mseedManager.fetchAllSeedNames();
        Log.v("ss", ""+seedStrings.length);
        //将可选内容与ArrayAdapter连接起来
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,seedStrings);        
        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        spinner1.setAdapter(adapter);
        //添加事件Spinner事件监听 
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				seedtype.setText(seedStrings[arg2]);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
		});
        //设置默认值
        spinner1.setVisibility(View.VISIBLE);
    }

    /**
     * 设置日期
     */
	private void setDateTime(){
       final Calendar c = Calendar.getInstance();  
       
       mYear = c.get(Calendar.YEAR);  
       mMonth = c.get(Calendar.MONTH);  
       mDay = c.get(Calendar.DAY_OF_MONTH); 
  
       updateDateDisplay(); 
       updateDateDisplay2();
	}
	
	/**
	 * 更新日期显示
	 */
	private void updateDateDisplay(){
       showDate.setText(new StringBuilder().append(mYear).append("-")
    		   .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
               .append((mDay < 10) ? "0" + mDay : mDay)); 
       
	}
	
	/**
	 * 更新日期显示
	 */
	private void updateDateDisplay2(){
       showDate2.setText(new StringBuilder().append(mYear).append("-")
    		   .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
               .append((mDay < 10) ? "0" + mDay : mDay)); 
	}
	
    /** 
     * 日期控件的事件 
     */  
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {  
  
       @Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,  int dayOfMonth) {  
           mYear = year;  
           mMonth = monthOfYear;  
           mDay = dayOfMonth;  

           updateDateDisplay();
       }  
    }; 
    
    /** 
     * 日期控件的事件 
     */  
    private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() {  
  
       @Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,  int dayOfMonth) {  
           mYear = year;  
           mMonth = monthOfYear;  
           mDay = dayOfMonth;  

           updateDateDisplay2();
       }  
    };
	
	/**
	 * 设置时间
	 */
	private void setTimeOfDay(){
	   final Calendar c = Calendar.getInstance(); 
       mHour = c.get(Calendar.HOUR_OF_DAY);
       mMinute = c.get(Calendar.MINUTE);
       updateTimeDisplay();
	}
	
	/**
	 * 更新时间显示
	 */
	private void updateTimeDisplay(){
       showTime.setText(new StringBuilder().append(mHour).append(":").append((mMinute < 10) ? "0" + mMinute : mMinute)); 
	}
    
    /**
     * 时间控件事件
     */
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			
			updateTimeDisplay();
		}
	};
    
    @Override  
    protected Dialog onCreateDialog(int id) {  
       switch (id) {  
	       case DATE_DIALOG_ID:  
	           return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
	       case TIME_DIALOG_ID:
	    	   return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
	       case DATE_DIALOG_ID2:
	           return new DatePickerDialog(this, mDateSetListener2, mYear, mMonth, mDay);
       }
    	   
       return null;  
    }  
  
    @Override  
    protected void onPrepareDialog(int id, Dialog dialog) {  
       switch (id) {  
	       case DATE_DIALOG_ID:  
	           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);  
	           break;
	       case TIME_DIALOG_ID:
	    	   ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
	    	   break;
	       case DATE_DIALOG_ID2:  
	           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);  
	           break;
       }
    }  
  
    /** 
     * 处理日期和时间控件的Handler 
     */  
    Handler dateandtimeHandler = new Handler() {
  
       @Override  
       public void handleMessage(Message msg) {  
           switch (msg.what) {  
	           case ExportSet.SHOW_DATAPICK:  
	               showDialog(DATE_DIALOG_ID);  
	               break; 
	           case ExportSet.SHOW_TIMEPICK:
	        	   showDialog(TIME_DIALOG_ID);
	        	   break;
	           case ExportSet.SHOW_DATAPICK2:  
	               showDialog(DATE_DIALOG_ID2);  
	               break;
           }  
       }  
  
    }; 
 
    @Override
	protected void onResume() {
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onPause();
	}
}